/**
 * Runs all 'Engine' object API tests in Host Object JSEngine.class
 */

module.id = "engine_tests";

exports.run = function() {
	doLoadTests();
};

Error.prototype.containsMessage = function(str) {
	return (this.message.indexOf(str) > -1);
};

function doLoadTests() {
	
	function noArgAssertion(result) {
		return ((result instanceof Error) && (result.containsMessage("At least one parameter is required")));
	};
	
	function firstArgStringAssertion(result) {
		return ((result instanceof Error) && (result.containsMessage("First argument provided must be a String")));
	};
	
	function secondArgStringAssertion(result) {
		return ((result instanceof Error) && (result.containsMessage("Second argument provided must be a String")));
	};
	
	function fileNotValidAssertion(result) {
		return ((result instanceof Error) && (result.containsMessage("not a valid file")));
	};
	
	function fileOutOfBoundsAssertion(result) {
		return ((result instanceof Error) && (result.containsMessage("File must be within the local application folder")));
	};

	
	var load = Engine.load;
	var tests = require('modules/tools/tester');
	
	tests.verbose(true);
	
	// No argumentss;
	tests.add(load, [], noArgAssertion);
	
	// First argument invalid;
	tests.add(load, [4], firstArgStringAssertion);
	tests.add(load, [undefined], firstArgStringAssertion);
	tests.add(load, [[1, 2, 3]], firstArgStringAssertion);
	tests.add(load, [null], firstArgStringAssertion);
	tests.add(load, [{"obj" : "key"}], firstArgStringAssertion);
	
	// Invalid filename;
	tests.add(load, [""], fileNotValidAssertion);
	tests.add(load, ["non_existant.js"], fileNotValidAssertion);
	
	// Filename provided is a directory, not a file!
	tests.add(load, ["scripts"], fileNotValidAssertion);
	tests.add(load, ["."], fileNotValidAssertion);
	tests.add(load, ["./"], fileNotValidAssertion);

	
	// Second argument invalid;
	tests.add(load, ["non_existant.js", 4], secondArgStringAssertion);
	tests.add(load, ["non_existant.js", undefined], secondArgStringAssertion);
	tests.add(load, ["non_existant.js", [1, 2, 3]], secondArgStringAssertion);
	tests.add(load, ["non_existant.js", null], secondArgStringAssertion);
	tests.add(load, ["non_existant.js", {"obj" : "key"}], secondArgStringAssertion);
	
	
	// /*
	 // * 
	 // * NOTE: This test case won't work for current program design.
	 // * The java method that calls the entry function is called from another thread.
	 // * Save for testing after it's redesigned.
	 // */
	// function invalidFunctionAssertion(result) {
		// return ((result instanceof Error) && (result.containsMessage("not a valid or callable Function")));
	// };
	// // First argument is valid, Invalid entry function in script;
	// tests.add(load, ["./scripts/random_script.js", "invalidFunc"], invalidFunctionAssertion);
	// tests.add(load, ["", "invalidEntryFunction"], invalidFunctionAssertion);
	
	
	/*
	 * The File being accessed is not in application-local filesystem!
	 */
	tests.add(load, ["../testfile.txt", "main"], fileOutOfBoundsAssertion);
	tests.add(load, ["/"], fileOutOfBoundsAssertion);	// Attempts root filesystem access;
	tests.add(load, ["\\"], fileOutOfBoundsAssertion); // Attempts root filesystem access;
	tests.add(load, ["C:\\"], fileOutOfBoundsAssertion); // Attempts root filesystem access;
	tests.add(load, ["../../../"], fileOutOfBoundsAssertion); // Attempts root filesystem access;
	tests.add(load, [".."], fileOutOfBoundsAssertion); 
	tests.add(load, ["../"], fileOutOfBoundsAssertion); 
	tests.add(load, ["C:\\windows\\system32\\"], fileOutOfBoundsAssertion);
	
	tests.run();
	
};